n, k = list(map(int, input().split()))
ans = 0
t = []
for _ in range(n):
l = list(map(int, input().split()))
if l[0] == 0:
ans += 1
else:
t += [set(l[1:])]
i = 0
while i < len(t):
for j in t[i + 1:]:
if t[i] & j:
t[i] |= j
t.remove(j)
break
else:
i += 1
print(max(0, len(t) - 1) + ans)
#include <iostream>
using namespace std;
int pre[105],vis[105];
void init(int n)
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int find(int x)
{
if(pre[x]==x) return x;
else return pre[x]=find(pre[x]);
}
int main()
{
int n,m,num,x1,x2,cnt=0,tcnt;
cin>>n>>m;
tcnt=m;
init(m);
for(int i=1;i<=n;i++)
{
cin>>num;
if(num==0) cnt++;
if(num!=0)
{
cin>>x1;
if(vis[x1]==0)
{
vis[x1]=1;tcnt--;
}
}
for(int j=2;j<=num;j++)
{
cin>>x2;
if(vis[x2]==0)
{
vis[x2]=1;tcnt--;
}
pre[find(x1)]=find(x2);
x1=x2;
}
}
for(int i=1;i<=m;i++)
if(pre[i]==i) cnt++;
if(tcnt==m) cout<<n;
else cout<<cnt-1-tcnt;
return 0;
}
468. Validate IP Address | 450. Delete Node in a BST |
445. Add Two Numbers II | 442. Find All Duplicates in an Array |
437. Path Sum III | 436. Find Right Interval |
435. Non-overlapping Intervals | 406. Queue Reconstruction by Height |
380. Insert Delete GetRandom O(1) | 332. Reconstruct Itinerary |
368. Largest Divisible Subset | 377. Combination Sum IV |
322. Coin Change | 307. Range Sum Query - Mutable |
287. Find the Duplicate Number | 279. Perfect Squares |
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |
222. Count Complete Tree Nodes | 215. Kth Largest Element in an Array |
198. House Robber | 153. Find Minimum in Rotated Sorted Array |
150. Evaluate Reverse Polish Notation | 144. Binary Tree Preorder Traversal |
137. Single Number II | 130. Surrounded Regions |